<%@include file="connection.jsp"%>

<%

	String userid = (String) session.getAttribute("userid");
		
    if (userid == null)
    {
       response.sendRedirect("login.jsp");
    }    
    
          Connection c = getConnection(session);
          Statement s = c.createStatement();
		  Statement s2 = c.createStatement();
		  
            String patient_id = request.getParameter("patient_id");
            String icd_code[] = request.getParameterValues("icd_code");
            String cpt_code[] = request.getParameterValues("cpt_code");
            
            String docCode= (String) session.getAttribute("Doc_Code");
            
			String diagCodes[] = new String[4];
			
			long invoiceNum = 1; 
			String primaryIns = "";
            String secondaryIns = "";
            //String BillingCycle = request.getParameter("BillingCycle");
			String BillingCycle = "CONTINUOUS";
            /*
			String CreatedDate = request.getParameter("CreatedDate");
			String CreatedBy = request.getParameter("CreatedBy");			
			String ModifiedDate = request.getParameter("ModifiedDate");
			String ModifiedBy = request.getParameter("ModifiedBy");
			*/	
			/****
				Saving selected ICD Codes in billing table
			****/			

            
            	/*
            		Storing selected diag codes in an array for inserting in the DB
            	*/
            	
                for (int i = 0; i < icd_code.length; i++) { 
                	diagCodes[i] = icd_code[i];
                }
                for (int i = icd_code.length; i < 4; i++) { 
                	diagCodes[i] = "";                 	
                }
                
                /*
	                Generating Invoice Number
                */             
                
                ResultSet rs = s.executeQuery("select max(InvoiceId) from tblbilling");
                if(rs.first()){
                	invoiceNum += rs.getLong(1);
                }
                
                /*
                	Getting Primary and secondary Insurance code from tblPatientInsurance table.
                */ 
                
                rs = s.executeQuery("select InsCode from tblpatientinsurance where PatientID='"+patient_id+"' and InsType='Primary'");
                if(rs.first()){
                	primaryIns = rs.getString(1);
                }
                
                rs = s.executeQuery("select InsCode from tblpatientinsurance where PatientID='"+patient_id+"' and InsType='Secondary'");
                if(rs.first()){
                	secondaryIns = rs.getString(1);
                }                
               
                s.executeUpdate("insert into tblbilling (BillID, InvoiceId, TransactionNumForConversion, PostingDate, PatID, DocID, DocID_R, Type,PatInsPrimary, PatInsSecondary, DiagnosisCode1, DiagnosisCode2, DiagnosisCode3, DiagnosisCode4, BillingCycle, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy) values('"+invoiceNum+"', '"+invoiceNum+"', '0', curdate(), '"+patient_id+"', '"+docCode+"', '', 'Regular', '"+primaryIns+"', '"+secondaryIns+"', '"+diagCodes[0]+"', '"+diagCodes[1]+"', '"+diagCodes[2]+"', '"+diagCodes[3]+"', '"+BillingCycle+"', curdate(), '"+userid+"', curdate(), '"+userid+"')");
            
            
            /****
					END Saving selected ICD Codes in billing table
			****/
			
			String from_date = request.getParameter("from_date");
			String to_date = request.getParameter("to_date");

			from_date = from_date.replace("/", "-");			
			String mm = from_date.substring(0,2);
			String dd = from_date.substring(3,5);
			String yy = from_date.substring(6,10);			
			from_date = yy+"-"+mm+"-"+dd;		

			
			to_date = to_date.replace("/", "-");
			
			mm = to_date.substring(0,2);
			dd = to_date.substring(3,5);
			yy = to_date.substring(6,10);
			
			to_date = yy+"-"+mm+"-"+dd;			
			
			String narration = request.getParameter("narration");
			/****
				Saving Procedures in billingDetail table
			****/
            if (cpt_code != null){
            	int itemCounter=0;
                for (int i = 0; i < cpt_code.length; i++) {                	
                  if(itemCounter == 6){                  
                  	itemCounter=0;
                  	invoiceNum++;
					s.executeUpdate("insert into tblbilling (BillID, InvoiceId, TransactionNumForConversion, PostingDate, PatID, DocID, DocID_R, Type,PatInsPrimary, PatInsSecondary, DiagnosisCode1, DiagnosisCode2, DiagnosisCode3, DiagnosisCode4, BillingCycle, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy) values('"+invoiceNum+"', '"+invoiceNum+"', '0', curdate(), '"+patient_id+"', '"+docCode+"', '', 'Regular', '"+primaryIns+"', '"+secondaryIns+"', '"+diagCodes[0]+"', '"+diagCodes[1]+"', '"+diagCodes[2]+"', '"+diagCodes[3]+"', '"+BillingCycle+"', curdate(), '"+userid+"', curdate(), '"+userid+"')");
                  }	
                  
                  String description="";
                  double standardPrice = 0;
                  
                  ResultSet rs2 = s2.executeQuery("select Description, StandardPrice from tblprocedure where CODE='"+cpt_code[i]+"'");
                  if(rs2.first()){
                  		description=rs2.getString(1);
	                	standardPrice = rs2.getDouble(2);
                  }
                  
                  s.executeUpdate("insert into tblbillingdetail (BillID, Line, DateFrom, DateThru, POS, ProcidureCode, CPT, M1, M2, M3,M4, Description, Units, Price, DX, PaidToDateDet, Narrative) values('"+invoiceNum+"', "+(itemCounter+1)+", '"+from_date+"', '"+to_date+"', 0, '"+cpt_code[i]+"', '"+cpt_code[i]+"', '', '', '', '', '"+description+"', 0 ,'"+standardPrice+"' ,0 ,0 ,'')");
                  itemCounter++;
                }
            }else{
            	s.executeUpdate("insert into tblbillingdetail (BillID, Line, DateFrom, DateThru, POS, ProcidureCode, CPT, M1, M2, M3,M4, Description, Units, Price, DX, PaidToDateDet, Narrative) values('"+invoiceNum+"', 1, '"+from_date+"', '"+to_date+"', 0, '', '', '', '', '', '', '', 0 ,0 ,0 ,0 ,'"+narration+"')");
            }
             
            s.close();
            s2.close();
            c.close();            
            
            response.sendRedirect("home.jsp?saved=1");
%>
<a href="home.jsp"/>Back</a>
